package com.yzmoney.common.action;

import com.yzmoney.common.constant.Constants;
import com.yzmoney.common.constant.PropertyUtils;
import com.yzmoney.common.model.ThirdPartyUserType;
import com.yzmoney.common.util.JDKStackTrace;
import com.yzmoney.wechat.model.token.AuthUserInfoJson;
import com.yzmoney.wechat.service.WechatAccessTokenService;
import com.yzmoney.wechat.service.WechatOAuthService;
import com.yzmoney.wechat.service.WechatUserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取用户信息
 * Created by DOVE on 2015/9/24.
 */
@Controller
@RequestMapping("/common")
public class UserInfoAction {

    private static Logger logger = Logger.getLogger(UserInfoAction.class);

    @Autowired
    private WechatOAuthService wechatOAuthService;
    @Autowired
    private WechatUserService wechatUserService;

    @Autowired
    private WechatAccessTokenService wechatAccessTokenService;

    @RequestMapping("loadOpenWechatUserInfo")
    @ResponseBody
    private AuthUserInfoJson loadOpenWechatUserInfo(
            String code) {
        String appId = PropertyUtils.getValue("kfAppID", "", Constants.GZH_PATH);
        String appSecret = PropertyUtils.getValue("kfAPPSecret", "", Constants.GZH_PATH);
        try {
            logger.info("开发平台获取扫码用户信息code：" + code + ",appId:" + appId + ",appSecret:" + appSecret);
            //根据code获取 access_token
            return loadJsAuthUserInfo(code, appId, appSecret);
        } catch (Exception e) {
            logger.warn("开发平台获取扫码用户信息失败!code：" + code + ",appId:" + appId + ",appSecret:" + appSecret + JDKStackTrace.getJDKStrack(e));
        }
        return null;
    }

    @RequestMapping(value = "loadWechatUserInfo", params = "code")
    @ResponseBody
    private AuthUserInfoJson loadWechatUserInfoByCode(
            String code) {
        try {
            logger.info("微信公众平台js auth2.0 获取用户信息!code：" + code);
            //根据code获取 AuthUserInfoJson
            AuthUserInfoJson authUserInfoJson = wechatOAuthService.loadWechatUserInfo(code);
            if (authUserInfoJson != null) {
                authUserInfoJson.setSubType(ThirdPartyUserType.WeixinSubType.PublicPlatform.ordinal());
            }
            return authUserInfoJson;
        } catch (Exception e) {
            logger.warn("微信公众平台js auth2.0 获取用户信息异常!code：" + code + JDKStackTrace.getJDKStrack(e));
        }
        return null;
    }

    @RequestMapping(value = "loadWechatUserInfo", params = "openId")
    @ResponseBody
    private AuthUserInfoJson loadWechatUserInfoByOpenId(String openId) {
        try {
            logger.info("获取微信用户!openId：" + openId);
            AuthUserInfoJson authUserInfoJson = wechatUserService.loadWechatUserInfo(openId);
            if (authUserInfoJson != null) {
                authUserInfoJson.setSubType(ThirdPartyUserType.WeixinSubType.PublicPlatform.ordinal());
            }
            return authUserInfoJson;
        } catch (Exception e) {
            logger.warn("获取微信用户异常!openId：" + openId + JDKStackTrace.getJDKStrack(e));
        }
        return null;
    }

    /**
     * 通过js 接口获取的微信信息 开放平台
     *
     * @return
     */
    private AuthUserInfoJson loadJsAuthUserInfo(String code, String appId, String appSecret) {
        AuthUserInfoJson authUserInfoJson = wechatOAuthService.loadUserInfo(wechatOAuthService.loadAccessToken(code, appId, appSecret));
        authUserInfoJson.setSubType(ThirdPartyUserType.WeixinSubType.Open.ordinal());
        return authUserInfoJson;
    }

}
